// components
import AlarmDialog from "@/components/AlarmDialog";

import Vue from "@/main";
import { globalStore } from "@/store/global";
import { ElNotification, ElMessage, ElMessageBox } from 'element-plus';
import { ref, reactive, getCurrentInstance, onMounted, watch, nextTick, h, markRaw, toRaw} from "vue";

function handleAlarmSubmit(form) {
  const globalVue = globalStore();
  const _this = Vue.config.globalProperties //vue3获取当前this
	return new Promise(function (resolve, reject) {
    if (!form.handleContent) {
      ElMessage({
        type: 'error',
        message: 'Please Enter Handling Content',
      });
      reject('Please Enter Handling Content');
      return;
    };
    let userInfo = globalVue.userInfo;
    form['userId'] = userInfo.userId;
    form['username'] = userInfo.username;
		_this.$http.post('archiveData.handleAlarm', form).then(res=>{
			resolve(res);
		}).catch(err => reject(err));
	});
};

// ElNotification 关闭事件
function notifyClose(data) {
  const globalVue = globalStore();
  globalVue.deleteElNotification({id: data.id});
};

/**
 * ElNotification 打开事件
 * @param {Object} data 弹框数据
 * @param {Number} duration 弹框显示时间 默认为10s
 */
export function notifyOpen(data, duration = 10000) {
  const globalVue = globalStore();
  return new Promise(function(resolve, reject) {
    if (data instanceof Array) {
      data && data.length && data.forEach(item => {
        globalVue.showNotify(item?.id) && setTimeout(() => {
          console.log('打开ID：', item.id);
          let notice = ElNotification({
            duration,
            offset: 60,
            dangerouslyUseHTMLString: true,
            message: h(AlarmDialog, {
              data: item, // 传递给子组件progress的prop
              // 事件要以onXxx的形式书写
              onSubmit: (form) => { 
                handleAlarmSubmit(form).then(res => {
                  ElMessage({
                    type: 'success',
                    message: 'Processed！',
                  });
                  notice.close(); // 关闭ElNotification
                  resolve(item);
                });
              },
            }),
            onClose: () => notifyClose(item)
          });
          globalVue.setElNotification({id: item.id, notify: notice});
        }, 0);
      });
    }else{
      if(!globalVue.showNotify(data?.id)) return;
      console.log('打开ID：', data.id);
      let notice = ElNotification({
        duration,
        offset: 60,
        dangerouslyUseHTMLString: true,
        message: h(AlarmDialog, {
          data, // 传递给子组件progress的prop
          // 事件要以onXxx的形式书写
          onSubmit: (form) => { 
            handleAlarmSubmit(form).then(res => {
              ElMessage({
                type: 'success',
                message: 'Processed！',
              });
              notice.close(); // 关闭ElNotification
              resolve(data);
            })
          },
        }),
        onClose: () => notifyClose(data)
      });
      globalVue.setElNotification({id: data.id, notify: notice});
    };
  })
};